package com.ybfenpan.controller.admin.info;


import com.ybfenpan.controller.core.BaseController;
import com.ybfenpan.model.Admin;
import com.ybfenpan.model.Message;
import com.ybfenpan.model.MessageReply;
import com.ybfenpan.service.MessageReplyService;
import com.ybfenpan.service.MessageService;
import com.ybfenpan.utils.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;


/**
 * Created by Administrator on 2016/3/25.
 */
@Controller

public class MessageShowController extends BaseController {
    @Resource
    private MessageService messageService;
    @Resource
    private MessageReplyService messageReplyService;
   
     
    @RequestMapping("admin/info/messageShow.shtml")
    public String show(HttpServletRequest request) throws Exception{
        Map formMap = getParameterMap(request);
        int id = MapUtil.getIntValue(formMap, "id");
        if(0 < id){
        	Message message = messageService.getByKey(id);
           
            request.setAttribute("message",message);
            
            formMap.put("messageId", id);
            List<MessageReply> list = this.messageReplyService.queryForList("messageReplyForList",formMap);
            
            request.setAttribute("list",list);
            
        }
        return "admin/info/messageShow";
       
    } 
    
    
    @RequestMapping("admin/info/messageShowAdd.shtml")
    public String showAdd(@RequestParam MultipartFile file, ModelMap model, HttpServletRequest request, HttpServletResponse response) throws Exception{
    	
        Map formMap = getParameterMap(request);
       
    	int id = MapUtil.getIntValue(formMap, "id");
	    if(0 < id){
	    	 
	    	Admin admin = ContextUtil.getAdmin();
	    	Message message = messageService.getByKey(id);
	        String myTwoPassword = MapUtil.getString(formMap, "myTwoPassword");
	    	
	    	String pwd = CryptUtil.getInstance().encrypt2String(myTwoPassword,CryptUtil.keyStr);
	    	 
	    	if(!admin.getTwoPassword().equals(pwd)){
	    		 
	    		setMessage(model,"安全密码不正确。");
	    		 
	    		request.setAttribute("message",message);
	             
	            formMap.put("messageId", id);
	            List<MessageReply> list = this.messageReplyService.queryForList("messageReplyForList",formMap);
	             
	            request.setAttribute("list",list);
	    		return "admin/info/messageShow";
	    		
	    	} else {
	        		
	        	String adminContent = MapUtil.getString(formMap, "adminContent");
	        	 
	    		if(file.getOriginalFilename()==null||file.getOriginalFilename()==""){
	    			Map mapMessage = new HashMap();
	    			mapMessage.put("id",id);
	    			mapMessage.put("type",20);
	            	messageService.update(mapMessage);
	            	
	              
	                MessageReply reply=new MessageReply();
	        		reply.setMessageId(id);			//所属服务单
	        		reply.setContent(adminContent);			//正文
	        		reply.setType(20);					//类型(10代理商回复，20管理员回复)
	        		reply.setAdminId(admin.getId());			//提交代理商
	        		
	        		String operIp=request.getRemoteHost();
	        		reply.setOperIp(operIp);				//提交IP
	        		reply.setOperDate(new Date());		//提交时间
	        		
	        		messageReplyService.saveOrUpdate(reply);
	        		
	        		return "redirect:messageList.shtml";
	        		
	    		}else{
	    			
	    			String docType = "message"; //保存的文件夹名称
	            	
	            	String fileName = file.getOriginalFilename();
	            	String docName =  FileUtils.getUUIDFileName(fileName);
	            	String newFile = SysConst.TMP_FILE + docName;
	               
	                String fileContentType=file.getContentType();
	                //后缀名
	    			String ext = fileName.substring(fileName.lastIndexOf('.')+1).toLowerCase();
	    		
	    			List<String> arrowType = Arrays.asList("images/gif","images/jpg","images/jpeg","images/pjpeg","images/png","images/bmp","image/gif","image/jpg","image/jpeg","image/pjpeg","image/png","image/bmp","image/x-png");
	    			List<String> arrowExt = Arrays.asList("gif","jpg","jpeg","png","bmp");
	    			
	    			
	    			if(!(arrowType.contains(fileContentType.toLowerCase()) && arrowExt.contains(ext))){
	    				 
	    				setMessage(model,"上传文件只接收gif/jpg/jpeg/png/bmp类型,您的文件类型为！"+fileContentType);
	    				 
		        		request.setAttribute("message",message);
		                 
		                formMap.put("messageId", id);
		                List<MessageReply> list = this.messageReplyService.queryForList("messageReplyForList",formMap);
		                 
		                request.setAttribute("list",list);
		        		return "admin/info/messageShow";
	    			}else {
	    				if(file.getSize()>(4*1024*1024)){
	    					setMessage(model,"上传图片不能大于4M。");
	    					request.setAttribute("message",message);
	    					 
	    	                formMap.put("messageId", id);
	    	                List<MessageReply> list = this.messageReplyService.queryForList("messageReplyForList",formMap);
	    	                 
	    	                request.setAttribute("list",list);
	    	        		return "admin/info/messageShow";
	    				}
	    				//文件存储到硬盘
	            		boolean flag = FileUtils.createFileToDisk(newFile, file.getInputStream());
	            		 
	                    if(flag){
	                    	
	                    	
	                    	Map mapMessage = new HashMap();
	                    	mapMessage.put("id",id);
	                    	mapMessage.put("type",20);
	                    	messageService.update(mapMessage);
	                    	
	                        String tmpFile = Tools.getWebPath(this.getClass())+SysConst.TMP_FILE+docName;
	                        
	                        FileUtils.copyFile(newFile,tmpFile);
	                        //FileUtils.copyFile(newFileTomcat1,tmpFile);
	                        //FileUtils.copyFile(newFileTomcat2,tmpFile);
	                        
	                        //网站域名
	                        String path = request.getContextPath();
	                    	String port = (0<request.getServerPort()) ? (":"+request.getServerPort()) : "";
	                    	String basePath = request.getScheme()+"://"+request.getServerName()+port+path+"/";
	                        
	                        
	                        MessageReply reply=new MessageReply();
	                		reply.setMessageId(id);			//所属服务单
	                		reply.setContent(adminContent);			//正文
	                		reply.setFileName(docName);			//附件名称
	                		reply.setFilePath(basePath+SysConst.TMP_FILE);			//附件路径
	                		
	                		reply.setType(20);					//类型(10代理商回复，20管理员回复)
	                		reply.setMemberId(admin.getId());			//提交代理商
	                		
	                		String operIp=request.getRemoteHost();
	                		reply.setOperIp(operIp);				//提交IP
	                		reply.setOperDate(new Date());		//提交时间
	                		
	                		messageReplyService.saveOrUpdate(reply);
	                		
	                		 
	                        return "redirect:messageList.shtml";
	                    }else{
	                    	 
	                    	setMessage(model,"上传图片失败");
	                    	request.setAttribute("message",message);
	   	                 
	    	                formMap.put("messageId", id);
	    	                List<MessageReply> list = this.messageReplyService.queryForList("messageReplyForList",formMap);
	    	                 
	    	                request.setAttribute("list",list);
	                    	return "admin/info/messageShow";
	                    }
	                    
	                    
					}
	           
	                
	    		}
		            	
		         
		        
	       }
	        	
	        	
		}
	    formMap.put("messageId", id);
        List<MessageReply> list = this.messageReplyService.queryForList("messageReplyForList",formMap);
         
        request.setAttribute("list",list);
        return "admin/info/messageShow";
       
    } 
}
